package com.cn.test;

import com.cn.mapper.CustomerMapper;
import com.cn.pojo.Customer;
import com.cn.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;

public class CustomerMapperTest1 {
    /**
     * 4.1 表字段别名和类属性名不一致，mybatis不能自动映射，返回null
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        Customer customer = customerMapper.queryCustomerById0(1);
        System.out.println(customer);
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 4.1.1 利用字段名实现自动映射
     */
    @Test
    public void test2() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        Customer customer = customerMapper.queryCustomerById1(1);
        System.out.println(customer);
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 4.1.2 利用字段名实现自动映射
     */
    @Test
    public void test3() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        Customer customer = customerMapper.queryCustomerById2(1);
        System.out.println(customer);
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 4.2 自定义结果映射resultMap
     */
    @Test
    public void test4() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
        Customer customer = customerMapper.queryCustomerById3(1);
        System.out.println(customer);
        SqlSessionUtil.closeSqlSession(sqlSession);
    }
}
